package com.simon.annual.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import com.simon.annual.entity.ReportNonrecurring;
import com.simon.annual.entity.ReportRetroactive;
import com.simon.annual.repositoy.IReportNonrecurringRepositoy;
import com.simon.annual.repositoy.IReportRetroactiveRepositoy;
import com.simon.annual.service.ICompanyPublicInfo;

/**
 * 第三节：会计数据和财务指标摘要
 * */
@Service
public class AccountingAndFinanceImpl {

	@Resource
	private ICompanyPublicInfo cmpanyPublicInfo;
	@Resource
	private IReportNonrecurringRepositoy reportNonrecurringRepositoy;
	@Resource
	private IReportRetroactiveRepositoy reportRetroactiveRepositoy;
	@Resource
	private ICompanyPublicInfo companyPublicInfo;
	
	public void analysis(Element na, int companyPtrId) {
//		Element  na = pElements.get(2);
		//盈利能力
		Element ee = companyPublicInfo.findNextTag("table", na);
		//偿债能力
		Element e1 = companyPublicInfo.findNextTag("table", ee);
		//营运情况
		Element e2 = companyPublicInfo.findNextTag("table", e1);
		//成长情况
		Element e3 = companyPublicInfo.findNextTag("table", e2);
		//股本情况
		Element e4 = companyPublicInfo.findNextTag("table", e3);
		//非经常性损益
		Element nonrecurring = companyPublicInfo.findNextTag("table", e4);
		List<ReportNonrecurring> list =new ArrayList<ReportNonrecurring>();
		Elements nonrecurringTr = nonrecurring.select("table tr");
		for (int i = 1; i < nonrecurringTr.size(); i++) {
			ReportNonrecurring non = new ReportNonrecurring();
			Elements tds = nonrecurringTr.get(i).children();
			non.setName(tds.get(0).text());
			non.setValue(tds.get(1).text());
			non.setProjectScopeId(companyPtrId);
			list.add(non);
		}
		reportNonrecurringRepositoy.save(list);
		//会计政策变更及会计差错更正等追溯调整或重述情况---选填项
		if(nonrecurring.nextElementSibling().text().indexOf("\u4F1A\u8BA1")>-1){
			Element retroactive = companyPublicInfo.findNextTag("table", nonrecurring);
			Elements retroactiveTr = retroactive.select("table tr");
			List<ReportRetroactive> reportRetroactiveList = new ArrayList<ReportRetroactive>();
			for (int i = 2; i < retroactiveTr.size(); i++) {
					ReportRetroactive reportRetroactive = new ReportRetroactive();
					Elements etds = retroactiveTr.get(i).children();
					reportRetroactive.setAcctName(etds.get(0).text());
					reportRetroactive.setThisPeriodBeforeRe(etds.get(1).text());
					reportRetroactive.setThisPeriodAfterRe(etds.get(2).text());
					reportRetroactive.setLastPeriodBeforeRe(etds.get(3).text());
					reportRetroactive.setLastPeriodAfterRe(etds.get(4).text());
//					reportRetroactive.setPeriodBeforeLastBeforeRe(etds.get(5).text());
//					reportRetroactive.setPeriodBeforeLastAfterRe(etds.get(6).text());
					reportRetroactive.setProjectScopeId(companyPtrId);
					reportRetroactiveList.add(reportRetroactive);
			}
			reportRetroactiveRepositoy.save(reportRetroactiveList);
		}
	}

}
